
#include <pthread.h>
#include <signal.h>

#include <logger.hpp>

#include "../tablet_server/service.hpp"

int main(int argc, char **argv)
{
	try {
		
	if(argc != 7)
	{
		std::cout << argv[0]
			  << " <thread_number> <io_service_number> <server_ip> <server_port> <client_ip> <client_port>"
			  << std::endl;
		return 1;
	}

	// Block all signals for background thread.
	sigset_t new_mask;
	sigfillset(&new_mask);
	sigset_t old_mask;
	pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask);

	xbase::application app(atoi(argv[1]),
			       atoi(argv[2]),
			       argv[3],
			       argv[4],
			       argv[5],
			       argv[6]);

	app.start();

	// Restore previous signals.
	pthread_sigmask(SIG_SETMASK, &old_mask, 0);

	// Wait for signal indicating time to shut down.
	sigset_t wait_mask;
	sigemptyset(&wait_mask);
	sigaddset(&wait_mask, SIGINT);
	sigaddset(&wait_mask, SIGQUIT);
	sigaddset(&wait_mask, SIGTERM);
	pthread_sigmask(SIG_BLOCK, &wait_mask, 0);
	int sig = 0;
	sigwait(&wait_mask, &sig);

	app.stop();
	app.join_all();

	}
	catch(const std::exception &e)
	{
		LOG(ERROR, "exception: " << e.what());
	}

	return 0;
}
